import AbstractBuffer from './AbstractBuffer';

interface BufferData {
    texture: WebGLTexture;
    vertices: number[];
    texCoords: number[];
}

class BoxBuffer extends AbstractBuffer {
    private _verticesBuffer: WebGLBuffer | null = null;
    private _texCoordBuffer: WebGLBuffer | null = null;
    private _texture?: WebGLTexture;
    verticesNum: number = 0;

    constructor(gl: WebGLRenderingContext) {
        super(gl);
        this._verticesBuffer = this._gl.createBuffer();
        this._texCoordBuffer = this._gl.createBuffer();
    }

    update(data: BufferData) {
        const { texture, vertices, texCoords } = data;
        this.verticesNum = vertices.length / 3;
        this._texture = texture;
        this._initArrayBuffer(this._verticesBuffer, vertices);
        this._initArrayBuffer(this._texCoordBuffer, texCoords);
    }

    bind(a_position: number, a_texCoord: number) {
        this._vertexAttribPointer(a_position, this._verticesBuffer, 3);
        this._vertexAttribPointer(a_texCoord, this._texCoordBuffer, 2);
        if (this._texture) {
            this._gl.bindTexture(this._gl.TEXTURE_2D, this._texture);
        }
    }
}

export default BoxBuffer;
